﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Xml.Serialization;
using System.Data;

namespace _1041326_1041339_1041433.AdminPages
{
    public partial class CommentAdmin : System.Web.UI.Page
    {
        String url = "http://localhost:1871/Service.svc";
        private String[] arr_status = new String[2] { "Không hoạt động", "Hoạt động" };

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Comment_List();
                lbThongBao.Text = "";
                Load_Control();
            }
        }

        protected void Load_Control()
        {
            btnThemMoi.Enabled = true;
            btnCapNhat.Enabled = false;
            btnLamLai.Enabled = false;
            btnHuy.Enabled = false;
        }

        protected void LoadControl()
        {
            btnThemMoi.Enabled = false;
            btnCapNhat.Enabled = true;
            btnLamLai.Enabled = true;
            btnHuy.Enabled = true;
        }

        private void Comment_List() {
            WebRequest wr = WebRequest.Create(url + "/comment/getAll");
            WebResponse wp = wr.GetResponse();
            XmlSerializer serial = new XmlSerializer(typeof(ArrayOfBinhLuanDTO));
            ArrayOfBinhLuanDTO ds = (ArrayOfBinhLuanDTO)serial.Deserialize(wp.GetResponseStream());

            #region note
            //DataTable dt = new DataTable();
            //dt.Columns.Add("#");
            //dt.Columns.Add("Người bình luận");
            //dt.Columns.Add("Tiêu đề tin");
            //dt.Columns.Add("Nội dung");
            //dt.Columns.Add("Tình trạng");
            //DataRow row = null;

            //for (int i = 0; i < ds.Items.Length; i++)
            //{
            //    row = dt.NewRow();
            //    row["#"] = (i + 1).ToString();
            //    row["Người bình luận"] = ds.Items[i].NguoiBinhLuan;
            //    row["Tiêu đề tin"] = ds.Items[i].MaTin;
            //    //  row["Họ tên"] = "<a href=' User_Update.aspx?email=" + ds.Items[i].Email + "'>" + ds.Items[i].Ten + "</a>";
            //    row["Nội dung"] = ds.Items[i].NoiDung;
            //    row["Tình trạng"] = arr_status[int.Parse(ds.Items[i].TinhTrang)];
            //    dt.Rows.Add(row);
            //} 
            #endregion
            gvComment.DataSource = ds.Items;
            gvComment.DataBind();
        }

        protected void gvComment_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectedIndex = gvComment.SelectedIndex;
            if (selectedIndex != -1)
            {
                LoadControl();
                txtTieuDe.Text = gvComment.Rows[selectedIndex].Cells[1].Text;
                txtNguoiDung.Text = gvComment.Rows[selectedIndex].Cells[2].Text;
                txtNoiDung.Text = gvComment.Rows[selectedIndex].Cells[3].Text;
                int trash = int.Parse(gvComment.Rows[selectedIndex].Cells[4].Text);
                rbHoatDong.Checked = (trash == 1);
                rbKhongHoatDong.Checked = !rbHoatDong.Checked;
            }
            else
            {
                lbThongBao.Text = "Vui lòng chọn giá trị để thao tác!";
            }
        }

        protected void Clean()
        {
            txtTieuDe.Text = "";
            txtNguoiDung.Text = "";
            txtNoiDung.Text = "";
            rbHoatDong.Checked = true;
        }

        protected void btnLamLai_Click(object sender, EventArgs e)
        {
            Clean();
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            Response.Redirect("Home.aspx");
        }

        protected void gvComment_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvComment.PageIndex = e.NewPageIndex;
            gvComment.DataBind();
        }
    }
}